﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.ExceptionHandling;
//using CSharp.Libs.WPF;
using CSharp.Libs.Koc;
using CSharp.Libs.Koc.Models;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace KocKernelTest
{
    class MainWindowViewModel : CSharp.Libs.WPF.ViewModelBase
    {
        #region declarations
        Dispatcher _Disp = Dispatcher.CurrentDispatcher;
        Kernel _KRNL = null;
        DatabaseOperations _DBOps = null;
        ObservableCollection<int> _lServerNumbers = new ObservableCollection<int>();
        #endregion
        


        #region constructor / finalizer
        public MainWindowViewModel()
        {
            //_lServerNumbers.Add(1);
            //_lServerNumbers.Add(6);
            //_lServerNumbers.Add(43);
            //_lServerNumbers.Add(12);

            _KRNL = new Kernel();


            //_DBOps = new DatabaseOperations();
            //_DBOps.InitDb(_KRNL.EC.XMLDebugFilePath + "\\" + _KRNL.LAO.GetOption(constants.LAO_AUTOKOC_DB_FILE_NAME));

            _KRNL.TM.SeedReceived += new TrafficMonitor.SeedReceivedEventHandler(TM_SeedReceived);
            _KRNL.TM.ChatReceived += new TrafficMonitor.ChatReceivedEventHandler(TM_ChatReceived);
        }
        #endregion
        
        
        
        #region event handling
        void TM_SeedReceived(object sender, CSharp.Libs.Koc.KernelModels.SeedReceivedEventArgs srea)
        {
            _ReceiveServerNr(srea.ServerNr);
        }

        void TM_ChatReceived(object sender, CSharp.Libs.Koc.KernelModels.ChatReceivedEventArgs crea)
        {
            _ReceiveServerNr(crea.ServerNr);
        }
        #endregion
        
        
        
        #region private functions
        private void _ReceiveServerNr(
            int ServerNumber)
        {
            try
            {
                if (!_lServerNumbers.Contains(ServerNumber))
                {
                    _Disp.BeginInvoke(new Action(() => _lServerNumbers.Add(ServerNumber)));
                    OnPropertyChanged("ServerNumbers");
                }

                //TEST
                //if (!_lServerNumbers.Contains(ServerNumber))
                //{
                //    _lServerNumbers.Add(ServerNumber);
                //    OnPropertyChanged("ServerNumbers");
                //}
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion
        
        
        
        #region interface
        #region properties
        public Kernel KRNL
        {
            get
            {
                return _KRNL;
            }
        }

        public ObservableCollection<int> ServerNumbers
        {
            get
            {
                return _lServerNumbers;
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
